<?php
/**
 *  md5签名，加密因子
 */
function getsignstr()
{
    return '879B839AF2D6BE77BB5838C56CB69B4B';
}
/**
 *  md5签名，$array中务必包含 appSecret
 */
function getsign($para)
{
    if(is_array($para))
    {
        $str='';
        foreach ($para as $k=>$v)
        {
            $str.=$k.'='.$v.'&';
        }
    }else
    {
        $str=$para;
    }
    $key=getsignstr();
    $str=substr($str,0,-1);
    $sign=md5($str.$key);
    return $sign;
}
/**
 * 直冲查询接口
 */
function getStatusRequest($ordernum)
{
    $timestamp=date('Y-m-d H:i:s',time());
    $url='http://ccapi.kabaling.com/Interface/Method';
    $array=array('customerid'=>803639,'customerorderno'=>$ordernum,'format'=>'format','method'=>'kamenwang.order.get','timestamp'=>$timestamp,'v'=>'1.0');
    $array['sign']=getsign($array);
    $result=curl_post($url,15,$array);
    return $result;
}
/**
 * 直冲接口
 */
function getresult($ordernum,$dhinfo,$productid,$money,$dhtype)
{
    $product_array=explode(',',$productid);
    if(count($product_array)>1)
    {
        $productid=getproductid($dhinfo,$product_array);
    }
    $url='http://ccapi.kamenwang.com/Interface/Method';
    $timestamp=date('Y-m-d H:i:s',time());
    $buynum=1;
    if($dhtype==1||$dhtype==2)//Q币,话费
    {
        $buynum=$money;
    }
	$array=array('buynum'=>$buynum,'chargeaccount'=>$dhinfo,'customerid'=>803652,'customerorderno'=>$ordernum,'format'=>'format','method'=>'kamenwang.order.add','notifyurl'=>HOST.'/index.php?tp=libs/fulu/exchange&op=getmsg','productid'=>$productid,'timestamp'=>$timestamp,'v'=>'1.0');   
    //file_put_contents(S_ROOT.'cache/day.txt',var_export($array,true),FILE_APPEND);
    $array['sign']=getsign($array);
    $result=curl_post($url,$array,15);
    return $result;
}
/**
 * 通过手机号获取运营商,返回对应的产品id
 */
function getproductid($dhinfo,$product_array)
{
    $CMCC=array(134,135,136,137,138,139,147,150,151,152,157,158,159,178,182,183,184,187,188);
    $CUCC=array(130,131,132,145,155,156,171,175,176,185,186);
    $CTCC=array(133,149,153,173,177,180,181,189);
    $three=substr($dhinfo,0,3);
    if(in_array($three,$CMCC))
    {
        $productid=$product_array[0];
    }elseif(in_array($three,$CUCC))
    {
        $productid=$product_array[1];
    }else
    {
        $productid=$product_array[2];
    }
    return $productid;
}
